package com.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springbootmybatisplus.mapper.PersonMapper;
import com.springbootmybatisplus.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PersonController {
    @Autowired
    private PersonMapper personMapper;

    @GetMapping("/test")
    public String test() {
        System.out.println(1L);
        return "1111111";
    }

    @GetMapping("/mybatisPlusTest")
    public String mybatisPlusTest() {
        //单个查
        Person person = personMapper.selectById("4011L");
        System.out.println(person);
//        批量查
        List<Person> people = personMapper.selectBatchIds(List.of(4015L, 4016L, 4017L, 4018L, 4019L));
        System.out.println(people);
        //更新
        Person p = new Person();
        p.setId(4015);
        p.setName("王冬冬");
        int i = personMapper.updateById(p);
        System.out.println("更新成功？："+i);
        //删除
        int i1 = personMapper.deleteById(4019);
        System.out.println("删除成功？："+i);
        return person.toString();
    }
    // 条件构造器Wrapper
    @GetMapping("/mybatisPlusTestRap")
    public String mybatisPlusTestRap() {
        QueryWrapper<Person> r = new QueryWrapper<>();
        //查那些字段 select
        r.select("id", "name","sex","age","nat");
        // 模糊查询
        r.like("name","霍");
        //大于等于：ge 大于：gt
        r.ge("age",22);
        //等于 eq
        r.eq("nat","安徽省");
        List<Person> people = personMapper.selectList(r);
        return people.toString();
    }
    // 条件构造器LambdaRap 避免硬编码，较少人为错误
    @GetMapping("/mybatisPlusTestLambdaRap")
    public String mybatisPlusTestLambdaRap() {
        LambdaQueryWrapper<Person> r = new LambdaQueryWrapper<Person>();
        //查那些字段 select
        r.select(person -> person.getId(),person -> person.getName(),person -> person.getAge(),person -> person.getNat());
        // 模糊查询
        r.like(person -> person.getName(),"霍");
        //大于等于：ge 大于：gt
        r.ge(person -> person.getAge(),22);
        //等于 eq
        r.eq(person -> person.getNat(),"安徽省");
        List<Person> people = personMapper.selectList(r);
        return people.toString();
    }
}
